<template>
	<view class="scheduling">
		<view class="content">
			<van-swipe-cell right-width="65">
				<view class="left">
					<view class="title">{{ sname }}
						
						<view class="pid">{{ pid }}</view>
					</view>	
					<view class="range">
						<view class="time">开始：{{ start }}</view>
						<view class="time">结束：{{ end }}</view>
					</view>		
					<view class="info">{{ amount }}{{ unit }} / 日</view>
				</view>
				
				<view slot="right" @click="del(gid1)" class="right">删除</view>
			</van-swipe-cell>
		</view>
		
	</view>
</template>

<script> 	
	import { delRecord as delRecordApi } from '@/api/emission'
	export default {
		props: {
			goodsList: {
				type: Array,
				default: []
			},
			gid1: {
				type: Number,
				default: 0
			},
			start: {
				Type: String,
				default: ''
			},
			end: {
				Type: String,
				default: ''
			},
			scale: {
				Type: Number,
				default: 0
			}
		},
		data() {
			return {
				cid: 0,
				gid: 0,
				sname: '',
				unit: '',
				pid: '',
				days: 0,
				amount: 0
			}
		},
		created() {	
			this.cid=this.$store.getters.getUser.cid
			for(let i=0; i<this.goodsList.length; i++) {
				if(this.goodsList[i].gid == this.gid1) {
					this.sname=this.goodsList[i].shortName
					this.unit=this.goodsList[i].unit
					this.pid=this.goodsList[i].pid
					this.days=this.getDaysBetween(this.end, this.start)+1
					this.amount=Math.floor(this.scale/this.days)					
					// console.log(this.days);
					break;
				}
			}
		},
		methods: {
			getDaysBetween(dateString1, dateString2) {
			    let startDate = Date.parse(dateString1);
			    let endDate = Date.parse(dateString2);
			    return (startDate - endDate ) / (1 * 24 * 60 * 60 * 1000);
			},
			async del(gid1) {
				// console.log('del',this.gid1)
				const data = {
					cid: this.cid,
					gid: this.gid1,
					startdate: this.start,
					enddate: this.end
				}
				
				const res = await delRecordApi(data)
				if(res.status === this.API_STATUS_CODE.SUCCESS) {
					this.state = false
					uni.showToast({
						icon: 'success',
						title: '删除成功',
						duration: 3000
					})
					this.timer = setTimeout(()=>{  //设置延迟执行
						uni.redirectTo({
							url: '../management/management'
						})
					},2000);
				}else {
					uni.showToast({
						icon: 'none',
						title: '删除失败，请刷新重试',
						duration: 3000
					})
				}
				
				// setTimeout(() => {
				// 	this.$router.go(0)
				// }, 500)
				// this.reload()
				// console.log(res)
			}
			
			
			//调用
			// getDaysBetween('2021-08-31', '2021-08-30')
			//结果为1天

		}
	}
</script>

<style lang="less" scoped>
	.scheduling {
		width: 100%;
		// padding: 20rpx;
		margin-bottom: 20rpx;
		box-sizing: border-box;
		
		.content {
			// height: 80rpx;
			// line-height: 50rpx;
			border-radius: 10rpx;
			box-sizing: border-box;
			background-color: rgba(255, 255, 255, 0.1);
		
			.left {
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				align-items: center;
				padding: 10rpx;
				
				box-sizing: border-box;
				width: 100%;
				// height: 100%;
				text-align: left;
				// background-color: pink;
				
				.title {
					width: 25%;
					color: #fff;
					font-weight: 700;
					font-size: 33rpx;
					text-align: center;
					
					
					.pid {
						width: 100%;
						font-weight: 400;
						// text-align: left;
					}
					
				}
				
				.range {
					display: flex;
					flex-wrap: wrap;
					width: 50%;
					// color: #b1c4fc;
					color: #fff;
					font-size: 28rpx;
					text-align: center;
					
					.time {
						width: 100%;
						margin-top: 10rpx;
					}
				}
				
				.info{
					width: 25%;
					color: #fff;
					font-size: 36rpx;
					text-align: right;
				}
			}
			
			.right {
				background-color: #ee0a24;
				width: 130rpx;
				height: 100%;
				text-align: center;
				line-height: 115rpx;
				color: #fff;
				// width: 130rpx;
				// height: 100%;
				
				// background-color: pink;
			}
		}
	}
</style>